<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarContactDao;
use app\model\car\CarContact;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\exception\ValidateException;
use think\facade\Route as Url;

/**
 * Class CarContactServices
 * @package app\services\shendeng
 */
class CarContactServices extends BaseServices
{
    public function __construct(CarContactDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        return $list;
    }

    /**
     * 添加修改
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $user = $this->dao->get($id);
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = Form::hidden('id', $id);
        $field[] = Form::input('name', '姓名', $user['name']??'');
        $field[] = Form::input('card_no', '身份证号', $user['card_no']??'');
        $field[] = Form::input('phone', '联系电话', $user['phone']??'')->maxlength(11);
        $field[] = Form::frameImage('card_a','身份证正面图',Url::buildUrl('admin/widget.images/index',array('fodder'=>'card_a')),$user['card_a']??'')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
        $field[] = Form::frameImage('card_b','身份证反面图',Url::buildUrl('admin/widget.images/index',array('fodder'=>'card_b')),$user['card_b']??'')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
        $field[] = Form::frameImage('car_cert','驾驶证正面图',Url::buildUrl('admin/widget.images/index',array('fodder'=>'car_cert')),$user['car_cert']??'')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
        $field[] = Form::frameImage('car_cert2','驾驶证副页图',Url::buildUrl('admin/widget.images/index',array('fodder'=>'car_cert2')),$user['car_cert2']??'')->icon('ios-add')->width('950px')->height('505px')->modal(['footer-hide' => true]);
        return create_form($title, $field, Url::buildUrl('/car/contact/save'), 'POST');
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        $user = $this->dao->getOne(['phone' => $data['phone'],'is_del'=>0]);
//        $data['uid'] = 0;
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($user && $id != $user['id']) throw new AdminException('该电话已绑定人员');
            if ($this->dao->update($id, $data)) {
                return true;
            } else {
                throw new AdminException('修改失败或者您没有修改什么');
            }
        } else {
            unset($data['id']);
            if ($user) throw new AdminException('该电话已绑定人员');
            $data['add_time'] = time();
            if ($this->dao->save($data)) {
                return true;
            } else {
                throw new AdminException('添加失败');
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delUser(int $id){
        $city = $this->dao->get($id);
        if ($city) {
            $data['is_del'] = 1;
            $data['delete_time'] = time();
            if (!$this->dao->update($id,$data)) {
                throw new AdminException('删除失败,请稍候再试!');
            }
        }
        return '删除成功!';
    }

    /**
     * 驾驶员资质提交审核
     * @param $id
     * @param $data
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function driverQualification($id,$data){
        /** @var CarBlacklistServices $carBlacklistServices */
        $carBlacklistServices = app()->make(CarBlacklistServices::class);
        $carBlacklistServices->verifyBlacklist(['phone'=>$data['phone'],'card_id'=>$data['card_no']]);
        $contact = CarContact::where(['is_del'=>0,'card_no'=>$data['card_no']])->select()->toArray();
        if($id){
            if (!$this->dao->get($id)) throw new ValidateException('未查询到该数据');
            if ($contact && $id != $contact[0]['id']) throw new ValidateException('该证件已被绑定');
            $data['status'] = 1;
            if($this->dao->update($id,$data)){
                return '修改成功';
            }else{
                throw new ValidateException('修改失败');
            }
        }else{
            if($contact) throw new ValidateException('该证件已被绑定');
            $data['status'] = 1;
            $data['add_time'] = time();
            if($this->dao->save($data)){
                return '提交成功';
            }else{
                throw new ValidateException('提交失败');
            }
        }
    }

    /**
     * 资质审核
     * @param int $id
     * @param array $data
     * @return string
     */
    public function auditSave(int $id,array $data){
        unset($data['id']);
        if($this->dao->update($id,$data)){
            return '审核成功';
        }else{
            throw new AdminException('审核失败');
        }
    }

    /**
     * 获取一条信息
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getContactInfo($id){
        $contact = $this->dao->get($id);
        $contact = $contact ? $contact->toArray() : [];
        return $contact;
    }
}
